﻿
Option Explicit On
Option Strict On

Imports System
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Serialization

Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages

Imports System.ComponentModel


Namespace ListViewNavigationWebPart

    <Guid("77b44fb0-5627-454b-ae42-92eea9fb0088"), Category("SPUMS.be")> _
    Public Class WebPart1
        Inherits System.Web.UI.WebControls.WebParts.WebPart


#Region "Properties"

        Private Const EXCLUDEDLISTDEVIDER As Char = CChar(";")

        Private _showDescription As Boolean
        <WebBrowsable(True), _
            Personalizable(True), _
            Category("Settings"), _
            DisplayName("Show description"), _
            WebDisplayName("Show description"), _
            Description("Show the list descriptions?")> _
        Public Property ShowDescription() As Boolean
            Get
                Return _showDescription
            End Get
            Set(ByVal value As Boolean)
                _showDescription = value
            End Set
        End Property

        Private _excludedListNames As String
        <WebBrowsable(True), _
            Personalizable(True), _
            Category("Settings"), _
            DisplayName("Excluded list names"), _
            WebDisplayName("Excluded list names"), _
            Description("The names of the lists that will be hidden. Use ; separate multiple lists.")> _
        Public Property ExcludedListNames() As String
            Get
                If Me._excludedListNames Is Nothing Then Me._excludedListNames = ""
                Return _excludedListNames
            End Get
            Set(ByVal value As String)
                _excludedListNames = value
            End Set
        End Property

        Private _showLists As Boolean
        <WebBrowsable(True), _
            Personalizable(True), _
            Category("Settings"), _
            DisplayName("Show lists"), _
            WebDisplayName("Show lists"), _
            Description("Show lists in the overview.")> _
        Public Property ShowLists() As Boolean
            Get
                Return _showLists
            End Get
            Set(ByVal value As Boolean)
                _showLists = value
            End Set
        End Property

        Private _showDocumentLibraries As Boolean
        <WebBrowsable(True), _
            Personalizable(True), _
            Category("Settings"), _
            DisplayName("Show documentlibraries"), _
            WebDisplayName("Show documentlibraries"), _
            Description("Show document libraries in the overview.")> _
        Public Property ShowDocumentLibraries() As Boolean
            Get
                Return _showDocumentLibraries
            End Get
            Set(ByVal value As Boolean)
                _showDocumentLibraries = value
            End Set
        End Property

#End Region

#Region "Constructor"
        Public Sub New()

        End Sub
#End Region

#Region "Methods"

        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()


            Dim HTML As New Text.StringBuilder

            Try
                'CSS
                HTML.Append(Me.GetCSS())


                HTML.Append("<ul>")
                For Each list As Microsoft.SharePoint.SPList In SPContext.Current.Web.Lists

                    If Me.ShowLists Then
                        If Not TypeOf list Is Microsoft.SharePoint.SPDocumentLibrary Then
                            If Me.ShowList(list) Then
                                HTML.Append(Me.GetHTML(list))
                            End If
                        End If
                    End If

                    If Me.ShowDocumentLibraries Then
                        If TypeOf list Is Microsoft.SharePoint.SPDocumentLibrary Then
                            If Me.ShowList(list) Then
                                HTML.Append(Me.GetHTML(list))
                            End If
                        End If
                    End If
                Next
                HTML.Append("</ul>")


            Catch ex As Exception

                Dim errorbox As New TextBox
                errorbox.Columns = 50
                errorbox.Rows = 10
                errorbox.TextMode = TextBoxMode.MultiLine
                errorbox.Text = ex.ToString

                Me.Controls.Add(errorbox)

            End Try

            Dim label As New Label()

            label.Text = HTML.ToString
            Me.Controls.Add(label)

        End Sub


        Private Function GetCSS() As String
            Dim HTML As New Text.StringBuilder

            'CSS
            HTML.Append("<style>")
            HTML.Append(".LVNWP_Bullet{")
            HTML.Append("   border-style: none;")
            HTML.Append("   border-color: inherit;")
            HTML.Append("   border-width: 0;")
            HTML.Append("   vertical-align:top;")
            HTML.Append("   background-image:url('/_layouts/images/lstbulet.gif');")
            HTML.Append("   background-repeat:no-repeat;")
            HTML.Append("   background-position:left;")
            HTML.Append("   padding-left:10px;")
            HTML.Append("   padding-right: 0px;")
            HTML.Append("} ")
            HTML.Append(".LVNWP_Desciption { ")
            HTML.Append("   font-size:8pt;  ")
            HTML.Append("   color: #4c4c4c; ")
            HTML.Append("} ")
            HTML.Append(".LVNWP_ListTitle {")
            HTML.Append("   font-weight:bold;")
            HTML.Append("}")
            HTML.Append("</style>")
            Return HTML.ToString
        End Function

        Private Function GetHTML(ByVal list As SPList) As String
            Dim HTML As New Text.StringBuilder
            HTML.Append(String.Format("<li><span class='LVNWP_ListTitle'><a href='{1}'>{0}</a></span>", list.Title, list.DefaultViewUrl))

            If Me.ShowDescription Then
                HTML.Append("<br /><span class='LVNWP_Desciption'>").Append(list.Description).Append("</span>")
            End If

            For Each view As Microsoft.SharePoint.SPView In list.Views
                If Not view.Hidden Then
                    HTML.Append("<div class='LVNWP_Bullet'><a href='").Append(view.ServerRelativeUrl).Append("'>").Append(view.Title).Append("</a></div>")
                End If
            Next


            HTML.Append("</li>")
            Return HTML.ToString

        End Function

        Private Function ShowList(ByVal list As SPList) As Boolean

            Try
                If Not list Is Nothing Then

                    If list.Hidden Then
                        Return False
                    End If

                    'check not in excluded lists
                    If Me.ExcludedListNames.Split(EXCLUDEDLISTDEVIDER).Length > 0 Then

                        For Each listname As String In Me.ExcludedListNames.Split(EXCLUDEDLISTDEVIDER)
                            If listname.ToLower = list.Title.ToLower Then
                                Return False
                            End If
                        Next

                        Return True

                    Else : Return True
                    End If

                End If
            Catch ex As Exception
                Return False
            End Try
        End Function

#End Region


    End Class

End Namespace
